/*  
 * Copyright @ 2020 xintuoinfo.com.cn
 * koala_platform_std 上午7:58:46  
 * All right reserved.  
 *  
*/
      
package com.xtsoft.platform.globe.utils;

import org.springframework.security.crypto.argon2.Argon2PasswordEncoder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.DelegatingPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.crypto.password.Pbkdf2PasswordEncoder;
import org.springframework.security.crypto.scrypt.SCryptPasswordEncoder;

import java.util.*;

/**  
 * <p> Spring Security5加密工具类  </p>
 * @author: defencez
 * @createTime: 2020年3月27日 上午7:58:46  
 * @since:  
 * @version: v1.0    
*/

public class EncryptUtil {
	
	public static String encrypt(String password,String encodingId) {
		return passwordEncoder(encodingId).encode(password);
	}
	
	/**
	 * 
	 * <p> 创建一个加密器  </p>  
	 * @param encodingId 加密方式Id
	 * @return
	 * @return PasswordEncoder
	 * @throws  
	 * @author defencez  
	 * @date 2020年3月27日 
	 * @time 上午8:07:12
	 */
	public static PasswordEncoder passwordEncoder(String encodingId) {
		//String encodingId = "bcrypt";
		Map<String, PasswordEncoder> encoders = new HashMap<>();
		encoders.put(encodingId, new BCryptPasswordEncoder());
		encoders.put("ldap", new org.springframework.security.crypto.password.LdapShaPasswordEncoder());
		encoders.put("MD4", new org.springframework.security.crypto.password.Md4PasswordEncoder());
		encoders.put("MD5", new org.springframework.security.crypto.password.MessageDigestPasswordEncoder("MD5"));
		encoders.put("noop", org.springframework.security.crypto.password.NoOpPasswordEncoder.getInstance());
		encoders.put("SHA-1", new org.springframework.security.crypto.password.MessageDigestPasswordEncoder("SHA-1"));
		encoders.put("SHA-256", new org.springframework.security.crypto.password.MessageDigestPasswordEncoder("SHA-256"));
		encoders.put("sha256", new org.springframework.security.crypto.password.StandardPasswordEncoder());
		PasswordEncoder passwordEncoder =
			    new DelegatingPasswordEncoder(encodingId, encoders);
		return passwordEncoder;
	} 


	public static void main(String[] args) {
		System.out.println(encrypt("123456", "bcrypt"));
		//演示Optional<T>的优点
		String var1 = null;
		//Optional<String> var = null;
		System.out.println("var1: " + Optional.ofNullable(var1).map(mapper->mapper).orElse("我是空的"));
		var1 = "我有值了";
		System.out.println("var1: " + Optional.ofNullable(var1).map(mapper->mapper).orElse("我是空的"));
		
		List<String> var2 = null;
		System.out.println("var2: " + Optional.ofNullable(var2).map(mapper->mapper.size()).orElseGet(() -> 100));
		var2 = new ArrayList(Arrays.asList("o1", "o2","o3")) ;		
		System.out.println("var2: " + Optional.ofNullable(var2).map(mapper->mapper.toString()).orElseGet(() -> "0000"));
		
		var1 = null;
		System.out.println("var1: " + Optional.ofNullable(var1).map(HttpClientUtil :: newFunction).orElse("我是空的"));
		var1 = "我有值";
		System.out.println("var1: " + Optional.ofNullable(var1).map(HttpClientUtil :: newFunction).orElse("我是空的"));		
		
		
		
	}
}
  
    